package com.riccardocardin.structured.concurrency.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.concurrent.ExecutionException;

import static com.riccardocardin.structured.concurrency.model.Model.*;

public class UserServiceImpl implements UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger("GitHubApp");


    @Override
    public User findUser(UserId userId) throws InterruptedException, ExecutionException {
        LOGGER.info("Finding user with id '{}'", userId);
        delay(Duration.ofMillis(500L));
        //throw new RuntimeException("Socket timeout");
        LOGGER.info("User '{}' found", userId);
        return new User(
                userId,
                new UserName("rcardin"),
                new Email("rcardin@rockthejvm.com"));
    }

    void delay(Duration duration) throws InterruptedException {
        Thread.sleep(duration);
    }
}
